<?php

class Subscriber_group extends Admin_Controller {

	public function __construct()
	{
        parent::__construct();

        $this->load->model(array('subscriber_group_model'));
    }

	public function add()
	{
		if (empty($_POST))
		{
			$this->template->load_partial("subscriber_group/add");
		}
		else
		{
            // VALIDATION
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', 'Gurup Adı', 'trim|required|htmlspecialchars|max_length[255]|callback__check_name');

			if (!$this->form_validation->run())
			{
				ajax_response($this->form_validation->error_string());
            }

            // CONTINUE

            $data = array(
                'user_id' => $this->auth->uid(),
                'name' => $this->input->post('name'),
                'date_add' => date('Y-m-d H:i:s')
            );

            $this->subscriber_group_model->add($data);

			ajax_response('İşlem Başarıyla Tamamlandı', FALSE);
        }
    }
	
	public function index()
	{
		$limit  = 20;
        $offset = _get('per_page', 0);
			
		$result = $this->subscriber_group_model->gets(array('user_id' => $this->auth->uid()), $limit, $offset);

        // PAGINATION
        $pagination = create_pagination(uri_string(), $result['total_rows'], $limit);

        // DATA
        $data = array(
            'result' => $result,
			'pagination' => $pagination
        );
		
		$this->template->load('subscriber_group/index', $data);
    }

    public function edit($id) {

        if (empty($_POST)) {

            // group
            $options = array('id' => $id, 'user_id' => $this->auth->uid());

            $group = $this->subscriber_group_model->get($options);

            // DATA
            $data = array(
                'group' => $group
            );

            // Load view
            $this->template->load_partial('subscriber_group/edit', $data);
        } else {
			
            // VALIDATION
            $this->load->library('form_validation');
            $this->form_validation->set_rules('group_id', 'Grup ID', 'trim|required|numeric');
            $this->form_validation->set_rules('name', 'Liste Adı', 'trim|required|htmlspecialchars|max_length[255]|callback__check_name');

			if (!$this->form_validation->run())
			{
				ajax_response($this->form_validation->error_string());
            }

            // CONTINUE

            $data = array(
                'name' => $this->input->post('name'),
            );

            // where
            $options = array(
                'id' => $this->input->post('group_id'),
                'user_id' => $this->auth->uid()
            );

            $this->subscriber_group_model->edit($data, $options);

			ajax_response('İşlem Tamamlandı', FALSE);
        }
    }

	public function delete($id)
	{
        $options = array(
            'id' => $id,
            'user_id' => $this->auth->uid()
        );

        $this->subscriber_group_model->delete($options);

		set_notify('İşlem başarıyla tamamlandı.');

        redirect($this->input->server('HTTP_REFERER'));
    }

	// Gurup adınının daha önce eklenip eklenmediğini kontrol eder
	public function _check_name()
	{
		$where = array(
			'name' => $this->input->post('name'),
			'user_id' => $this->auth->uid()
		);
		
		if($this->db->where($where)->count_all_results('subscriber_group'))
		{
			$this->form_validation->set_message('_check_name', 'Bu isimde zaten bir gurup var');
			return FALSE;	
		}

		return TRUE;
	}
}

/* End of file mail_group.php */
